<?php
/**
 * Extension of SQLColumnDefinitionClause providing vendor-specific functionality as well as resolving clauses into string (which in this case are always vendor-specific).
 */
class MySQLAlterClause extends SQLAlterClause {
	public static function getInstance() {
		return new MySQLAlterClause();
	}
	
	/**
	 * Renames column in table.
	 * 
	 * @param string $strOldColumnName
	 * @param SQLColumnDefinitionClause $objNewColumnDefinition
	 * @param string $strColumnNameBefore
	 * @return SQLAlterClause
	 */
	public function renameColumn2($strOldColumnName, $objNewColumnDefinition, $strColumnNameBefore="") {
		$tblTMP = array();
		$tblTMP["TYPE"] = "COLUMN";
		$tblTMP["OPERATION"] = "RENAME";
		$tblTMP["COLUMN"] = $objNewColumnDefinition;
		$tblTMP["OLD_COLUMN_NAME"] = $strOldColumnName;
		if($strColumnNameBefore) $tblTMP["BEFORE_COLUMN"] = $strColumnNameBefore;
		$this->tblContents[]=$tblTMP;
		return this;
	}
	
	/**
	 * Translates clause into string.
	 * 
	 * (non-PHPdoc)
	 * @see AbstractSQLClause::toString()
	 */
	public function toString() {
		$output = "";
		
		foreach($this->tblContents as $tblValues) {
			$output="";
			if($tblValues['TYPE']=="COLUMN") {
				if($tblValues['OPERATION']=="ADD") {
					$output .= "ADD COLUMN ".$tblValues["COLUMN"]->toString();
					if(isset($tblValues["BEFORE_COLUMN"])) $output .= " FIRST ".$tblValues['BEFORE_COLUMN'];
				} else if($tblValues['OPERATION']=="CHANGE") {
					$output .= "MODIFY COLUMN ".$tblValues["COLUMN"]->toString();
					if(isset($tblValues["BEFORE_COLUMN"])) $output .= " FIRST ".$tblValues['BEFORE_COLUMN'];
				} else if($tblValues['OPERATION']=="RENAME") {
					$output .= "CHANGE COLUMN ".$tblValues["OLD_COLUMN_NAME"]." ".$tblValues["COLUMN"]->toString();
					if(isset($tblValues["BEFORE_COLUMN"])) $output .= " FIRST ".$tblValues['BEFORE_COLUMN'];
				} else if($tblValues['OPERATION']=="DROP") {
					$output .= "DROP COLUMN ".$tblValues["NAME"];
				} 
			} else if($tblValues['TYPE']=="KEY") {
				if($tblValues['OPERATION']=="ADD") {
					$output .= "ADD KEY ".(isset($tblValues["NAME"])?$tblValues["NAME"]:"")." (".$tblValues["COLUMNS"]->toString().")";
				} else if ($tblValues['OPERATION']=="DROP") {
					$output .= "DROP INDEX ".$tblValues["NAME"];
				} 
			} else if($tblValues['TYPE']=="UNIQUE_KEY") {
				if($tblValues['OPERATION']=="ADD") {
					$output .= "ADD UNIQUE KEY ".(isset($tblValues["NAME"])?$tblValues["NAME"]:"")." (".$tblValues["COLUMNS"]->toString().")";
				} else if ($tblValues['OPERATION']=="DROP") {
					$output .= "DROP INDEX ".$tblValues["NAME"];
				} 
			} else if($tblValues['TYPE']=="PRIMARY_KEY") {
				if($tblValues['OPERATION']=="ADD") {
					$output .= "ADD PRIMARY KEY (".$tblValues["COLUMNS"]->toString().")";
				} else if ($tblValues['OPERATION']=="DROP") {
					$output .= "DROP PRIMARY KEY";
				} 
			} else if($tblValues['TYPE']=="FOREIGN_KEY") {
				if($tblValues['OPERATION']=="ADD") {
					$output .= "ADD FOREIGN KEY ".(isset($tblValues["NAME"])?$tblValues["NAME"]:"")." (".$tblValues["COLUMN"].")";
					if(isset($tblValues["DELETE_ACTION"])) $output .= " ON DELETE ".$tblValues["DELETE_ACTION"];
					if(isset($tblValues["UPDATE_ACTION"])) $output .= " ON UPDATE ".$tblValues["UPDATE_ACTION"];
				} else if ($tblValues['OPERATION']=="DROP") {
					$output .= "DROP FOREIGN KEY ".$tblValues["NAME"];
				} 
			} else if($tblValues['TYPE']=="AUTO_INCREMENT") {
				$output .= "AUTO_INCREMENT = ".$tblValues["VALUE"];
			} else if($tblValues['TYPE']=="ENGINE") {
				$output .= "ENGINE = ".$tblValues["VALUE"];
			}
			$output .= ", ";
		}
		return $output;
	}
	
}